package org.jeecg.modules.hxrencai.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.hxrencai.bo.AreaBo;
import org.jeecg.modules.hxrencai.dto.StudyParkDto;
import org.jeecg.modules.hxrencai.entity.StudyPark;
import org.jeecg.modules.hxrencai.service.IStudyParkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 园区/研究院信息
 * @Author: jeecg-boot
 * @Date: 2019-11-24
 * @Version: V1.0
 */
@Slf4j
//@Api(tags = "园区/研究院信息")
//@RestController
//@RequestMapping("/hxrencai/studyPark")
public class StudyParkController extends JeecgController<StudyPark, IStudyParkService> {
    @Autowired
    private IStudyParkService studyParkService;

    /**
     * 分页列表查询
     *
     * @param req
     * @return
     */
//    @AutoLog(value = "园区/研究院信息-分页列表查询")
//    @ApiOperation(value = "园区/研究院信息-分页列表查询  3 全国园区 4 全国研究 ", notes = "园区/研究院信息-分页列表查询")
//    @GetMapping(value = "/list")
    public Result<?> queryPageList(StudyParkDto dto,
                                   HttpServletRequest req) {
        StudyPark studyPark = new StudyPark();
        BeanUtil.copyProperties(dto, studyPark);
        QueryWrapper<StudyPark> queryWrapper = QueryGenerator.initQueryWrapper(studyPark, req.getParameterMap());
        int pageNo = dto.getPageNo() == null ? 1 : dto.getPageNo();
        int pageSize = dto.getPageSize() == null ? 10 : dto.getPageSize();
        Page<StudyPark> page = new Page<StudyPark>(pageNo, pageSize);
        if (StringUtils.isBlank(studyPark.getImportType())) {
            queryWrapper.eq("import_type", "3");
        }
        IPage<StudyPark> pageList = studyParkService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    //    @AutoLog(value = "园区/研究院 省份数量统计")
//    @ApiOperation(value = "园区省份数量统计 ", notes = "园区省份数量统计")
//    @GetMapping(value = "/queryProvinceNumByArea/{importType}")
    public List<AreaBo> queryProvinceNumByArea(@ApiParam(required = true, value = "导入类型 3 全国园区 4 全国研究") @PathVariable String importType) {
        return studyParkService.queryProvinceNumByArea(importType);
    }


    //    @AutoLog(value = "园区-获取所有")
//    @ApiOperation(value = "园区-获取所有", notes = "园区-获取所有")
//    @GetMapping(value = "/getYqAll")
    public List<StudyPark> getYqAll() {
        LambdaQueryWrapper<StudyPark> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(StudyPark::getImportType, "3");
        List<StudyPark> list = studyParkService.list(queryWrapper);
        return list;
    }

    //    @AutoLog(value = "研究院-获取所有")
//    @ApiOperation(value = "研究院-获取所有", notes = "研究院-获取所有")
//    @GetMapping(value = "/getYjyAll")
    public List<StudyPark> getYjyAll() {
        LambdaQueryWrapper<StudyPark> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(StudyPark::getImportType, "4");
        List<StudyPark> list = studyParkService.list(queryWrapper);
        return list;
    }


    /**
     * 添加
     *
     * @param studyPark
     * @return
     */
//    @AutoLog(value = "园区/研究院信息-添加")
    //@ApiOperation(value = "园区/研究院信息-添加", notes = "园区/研究院信息-添加")
//    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody StudyPark studyPark) {
        studyParkService.save(studyPark);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param studyPark
     * @return
     */
//    @AutoLog(value = "园区/研究院信息-编辑")
    //@ApiOperation(value = "园区/研究院信息-编辑", notes = "园区/研究院信息-编辑")
//    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody StudyPark studyPark) {
        studyParkService.updateById(studyPark);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
//    @AutoLog(value = "园区/研究院信息-通过id删除")
    //@ApiOperation(value = "园区/研究院信息-通过id删除", notes = "园区/研究院信息-通过id删除")
//    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        studyParkService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
//    @AutoLog(value = "园区/研究院信息-批量删除")
    //@ApiOperation(value = "园区/研究院信息-批量删除", notes = "园区/研究院信息-批量删除")
//    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.studyParkService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
//    @AutoLog(value = "园区/研究院信息-通过id查询")
//    @ApiOperation(value = "园区/研究院信息-通过id查询", notes = "园区/研究院信息-通过id查询")
//    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        StudyPark studyPark = studyParkService.getById(id);
        return Result.ok(studyPark);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param studyPark
     */
//    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, StudyPark studyPark) {
        return super.exportXls(request, studyPark, StudyPark.class, "园区/研究院信息");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, StudyPark.class);
    }

}
